package com.csu.hemiao.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;


import java.io.Serializable;

//这里<t>表示使用一个泛型，因为response返回的是category还是product，是对象还是list都是不确定的
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> implements Serializable {

    private int status; //一定会有
    private String msg; //有时会没有
    private T data; //有时会没有

    @JsonIgnore
    public boolean isSuccess(){
        return this.status == com.csu.hemiao.common.ResponseCode.SUCCESS.getCode();
    }

    private CommonResponse(int status){
        this.status = status;
    }

    private CommonResponse(int status, String msg){ //即使只传参String，也是调用这个方法
        this.status = status;
        this.msg = msg;
    }

    private CommonResponse(int status, String msg, T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private CommonResponse(int status, T data){
        this.status = status;
        this.data = data;
    }

    public static <T> CommonResponse<T> createForSuccess(){
        return new CommonResponse<T>(com.csu.hemiao.common.ResponseCode.SUCCESS.getCode());
    }

    public static <T> CommonResponse<T> createForSuccess(T data){
        return new CommonResponse<T>(com.csu.hemiao.common.ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> CommonResponse<T> createForSuccessMessage(String msg){
        return new CommonResponse<T>(com.csu.hemiao.common.ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> CommonResponse<T> createForSuccessMessageData(String msg, T data){
        return new CommonResponse<T>(com.csu.hemiao.common.ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> CommonResponse<T> createForError(){
        return new CommonResponse<T>(com.csu.hemiao.common.ResponseCode.ERROR.getCode(), com.csu.hemiao.common.ResponseCode.ERROR.getDescription());
    }

    public static <T> CommonResponse<T> createForError(String msg){
        return new CommonResponse<T>(com.csu.hemiao.common.ResponseCode.ERROR.getCode(), msg);
    }

    public static <T> CommonResponse<T> createForError(int code, String msg){
        return new CommonResponse<T>(code, msg);
    }
}
